import React, {Fragment, PureComponent} from 'react';
import {Checkbox, Col, Form, Input, Modal, Radio, Row} from 'antd';
import moment from 'moment';
import {decode, } from '@/utils/AesUtil';
import {getUser} from '@/utils/bwdautil';
import styles from '@/utils/utils.less';

const FormItem = Form.Item;
const {TextArea} = Input;
const CheckboxGroup = Checkbox.Group;
const RightOptions = [
  {label: '加密', value: '1'},
  {label: '脱敏', value: '3'},
  {label: '转PDF', value: '4'},
  {label: '水印', value: '5'},
];
//水印控制
// const WaterOptions = [
//   {label: '明文水印', value: '1'},
//   {label: '屏幕水印', value: '2'},
//   // {label: '暗水印', value: '3'},
//   // {label: '打印水印', value: '4'},
// ];

// 1明文水印2屏幕水印3暗水印4打印水印

// 申请详情页面，包含审核
@Form.create()
class ApplyDetail extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      currentStep: 0,
    };
    this.formLayout = {
      labelCol: {span: 6},
      wrapperCol: {span: 18},
    };
    this.colsLayout = {
      labelCol: {span: 3},
      wrapperCol: {span: 21},
    };
  }

  // 整合成一个弹框
  renderAllStep() {
    const {form, record, isAudit, waterOptions, authOptions} = this.props;
    let rightValue = [];
    if (record && record.outVo && record.outVo.rightConfig) {
      rightValue = record.outVo.rightConfig.split(",");
    }

    let waterValue = [];
    if (record && record.outVo && record.outVo.waterType) {
      waterValue = record.outVo.waterType.split(",");
    }

    const applyType = record.applyType;
    //流转对象
    let targets = "";
    if (record && record.flowVo) {
      record.flowVo.forEach(item => {
        targets = targets + item.userName + ',';
      })
      targets = targets.substring(0, targets.length - 1);
    }
    let filePwd = "***";
    if (record && record.outVo && record.outVo.filePwd) {
      //如果是本人则进行解密
      let user = getUser();
      if (user && user.userCode && (user.userCode == record.applyUserCode)) {
        filePwd = record.outVo.filePwd
      }
    } else {
      filePwd = "无密码"
    }

    return <Row>
      <Col md={12}>
        <FormItem key="fileDirNames" {...this.formLayout} label="文件名称">
          {record.fileNames}
        </FormItem>
      </Col>
      <Col md={12}>
        <FormItem key="auditUserName" {...this.formLayout} label="申请人">
          {record.applyUserName}
        </FormItem>
      </Col>
      <Col md={12}>
        <FormItem key="createTime" {...this.formLayout} label="申请时间">
          {moment(record.createTime).format('YYYY-MM-DD HH:mm:ss')}
        </FormItem>
      </Col>
      <Col md={12}>
        <FormItem key="applyTitle" {...this.formLayout} label="申请标题">
          {record.applyTitle}
        </FormItem>
      </Col>
      <Col md={24}>
        <FormItem key="applyReason" {...this.colsLayout} label="申请理由">
          {record.applyReason}
        </FormItem>
      </Col>
      {/*{applyType == 1 ? <FormItem key="applyTime" {...this.formLayout} label="共享人">*/}
      {/*{record.applyUserName}*/}
      {/*</FormItem> : null}*/}
      { //流转对象
        applyType == 2 ? <Col md={24}><FormItem key="applyReason" {...this.colsLayout} label="接收人">
          {targets}
        </FormItem></Col> : null}
      {/*{ //权限管控*/}
      {/*applyType == 2 ? <FormItem  {...this.formLayout} label="权限管控">*/}
      {/*<CheckboxGroup disabled value={record.rightConfig||[]} options={RightOptions}/>*/}
      {/*</FormItem>: null}*/}
      {applyType == 3 ?
        <Fragment>
          <Col md={24}>
            <FormItem key="lifeCyle" {...this.colsLayout} label="阅读时限">
              开始时间：{record && record.outVo && record.outVo.startTime ? moment(record.outVo.startTime).format('YYYY-MM-DD HH:mm:ss') : '无限制'}，
              结束时间：{record && record.outVo && record.outVo.endTime ? moment(record.outVo.endTime).format('YYYY-MM-DD HH:mm:ss') : '无限制'}
            </FormItem>
          </Col>
          <Col md={12}>
            <FormItem key="readTimes" {...this.formLayout} label="阅读次数">
              {record && record.outVo && record.outVo.readTimes ? record.outVo.readTimes : '无限制'}
            </FormItem>
          </Col>
          <Col md={12}>
            <FormItem key="filePwd" {...this.formLayout} label="打开密码">
              {filePwd}
            </FormItem>
          </Col>
          <Col md={24}>
            <FormItem key="right" {...this.colsLayout} label="权限控制">
              {form.getFieldDecorator('right', {initialValue: rightValue})(
                <CheckboxGroup disabled options={authOptions}/>
              )}
            </FormItem>
          </Col>
          <Col md={24}>
            {/* 1:明文水印 2:屏幕水印 3:暗水印 4:打印水印*/}
            <FormItem key="waterType" {...this.colsLayout} label="水印控制">
              {form.getFieldDecorator('waterType', {initialValue: waterValue})(
                <CheckboxGroup disabled options={waterOptions}/>
              )}
            </FormItem>
          </Col>
          <Col md={12}>
            <FormItem key="sendType" {...this.formLayout} label="下载方式">
              {record && record.outVo && record.outVo.sendType == 0 ? '手动下载' : '邮件下载'}
            </FormItem>
          </Col>
          <Col md={12}>
            <FormItem key="fileType" {...this.formLayout} label="外发类型">
              {record && record.outVo && record.outVo.fileType == 2 ? '文件包' : 'exe可执行文件'}
            </FormItem>
          </Col>
        </Fragment> : null}
      {isAudit ?
        <Fragment>
          <Col md={12}>
            <FormItem key="applyState" {...this.formLayout} label="审批结果">
              {form.getFieldDecorator('applyState', {rules: [{required: true, message: '审批结果不能为空'}]})(
                <Radio.Group>
                  <Radio value={1}>
                    通过
                  </Radio>
                  <Radio value={2}>
                    拒绝
                  </Radio>
                </Radio.Group>
              )}
            </FormItem>
          </Col>
          <Col md={12}>
            <FormItem key="remark" {...this.formLayout} label="备注">
              {form.getFieldDecorator('remark')(
                <TextArea
                  style={{minHeight: 32}}
                  rows={2}
                />
              )}
            </FormItem>
          </Col>
        </Fragment> : (record.applyState == 0 ?
          <Fragment>
            {/*详情页，审核中*/}
            <Col md={12}>
              <FormItem key="applyTime" {...this.formLayout} label="审批结果">
                审批中
              </FormItem>
            </Col>
          </Fragment> : <Fragment>
            {/*详情页，已审核*/}
            <Col md={12}>
              <FormItem key="applyTime" {...this.formLayout} label="审批结果">
                {record.applyState == 1 ? "通过" : "拒绝"}
              </FormItem>
            </Col>
            <Col md={12}>
              <FormItem key="remark" {...this.formLayout} label="备注">
                {record.auditRemark ? record.auditRemark : record.remark}
              </FormItem>
            </Col>
          </Fragment>)}
    </Row>
  }

  okHandle = () => {
    const {form, isAudit, handleApplyAudit, handleModalVisible, record} = this.props;
    if (isAudit) {
      const {formVals: oldValue} = this.state;
      form.validateFields((err, fieldsValue) => {
        if (err) return;
        const formVals = {...oldValue, ...fieldsValue};
        this.setState(
          {
            formVals,
          },
          () => {
            handleApplyAudit(formVals, record);
          }
        );
      });
    } else {
      //详情页直接取消就是了
      handleModalVisible();
    }
  }

  render() {
    const {modalVisible, form, handleModalVisible, record, handleApplyAudit, isAudit} = this.props;
    //当前页面状态
    const {currentStep, formVals} = this.state;
    return (
      <Modal
        width={900}
        bodyStyle={{padding: '0'}}
        destroyOnClose
        title="申请详情"
        visible={modalVisible}
        onOk={() => this.okHandle()}
        onCancel={() => handleModalVisible()}
        afterClose={() => handleModalVisible()}
        maskClosable={false}
        className={styles.viewMore}
      >
        {this.renderAllStep()}
      </Modal>
    );
  }
}

export default ApplyDetail;
